package com.fan.gofast.util;

import org.apache.commons.lang3.StringUtils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.StringTokenizer;

/**
 * 获得linux服务器IP
 *
 */

public class IpUtil {

    public static String getServerIpAddress() {
        try {
            //先找eth0的网卡IP,没有则重新循环
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                String networkCardName = networkInterface.getName();
                if ("eth0".equalsIgnoreCase(networkCardName)) {
                    Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                    while (inetAddresses.hasMoreElements()) {
                        InetAddress inetAddress = inetAddresses.nextElement();
                        if (inetAddress != null && inetAddress instanceof Inet4Address) { // IPV4
                            return inetAddress.getHostAddress();
                        }
                    }
                }
            }
            Enumeration<NetworkInterface> networkInterfaces1 = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces1.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces1.nextElement();
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    String inetAddressIp = inetAddress.getHostAddress();
                    if ((inetAddressIp.endsWith(".0")) || (inetAddressIp.endsWith(".1"))) {
                        continue;
                    }
                    if (inetAddress.isSiteLocalAddress() && !inetAddress.isLoopbackAddress() // 127.开头的都是lookback地址
                            && inetAddress.getHostAddress().indexOf(":") == -1 &&
                            inetAddress instanceof Inet4Address) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] getServerIpAddressByteArray() {
        try {
            //先找eth0的网卡IP,没有则重新循环
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                String networkCardName = networkInterface.getName();
                if ("eth0".equalsIgnoreCase(networkCardName)) {
                    Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                    while (inetAddresses.hasMoreElements()) {
                        InetAddress inetAddress = inetAddresses.nextElement();
                        if (inetAddress != null && inetAddress instanceof Inet4Address) { // IPV4
                            return inetAddress.getAddress();
                        }
                    }
                }
            }
            Enumeration<NetworkInterface> networkInterfaces1 = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces1.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces1.nextElement();
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    String inetAddressIp = inetAddress.getHostAddress();
                    if ((inetAddressIp.endsWith(".0")) || (inetAddressIp.endsWith(".1"))) {
                        continue;
                    }
                    if (inetAddress.isSiteLocalAddress() && !inetAddress.isLoopbackAddress() // 127.开头的都是lookback地址
                            && inetAddress.getHostAddress().indexOf(":") == -1 &&
                            inetAddress instanceof Inet4Address) {
                        return inetAddress.getAddress();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int ip2Int(String ip) {
        if (StringUtils.isBlank(ip)) {
            return 0;
        }
        int result = 0;
        StringTokenizer token = new StringTokenizer(ip, ".");
        if (token.countTokens() != 4) {
            return 0;
        }
        result |= Integer.parseInt(token.nextToken()) << 24;
        result |= Integer.parseInt(token.nextToken()) << 16;
        result |= Integer.parseInt(token.nextToken()) << 8;
        result |= Integer.parseInt(token.nextToken());
        return result;
    }


    public static void main(String[] args) {
        String s = getServerIpAddress();
        System.out.println(s);
        int p = ip2Int("10.0.11.201");
        System.out.println(p);
        System.out.println(p & ((1 << 16) - 1));
    }
}
